/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.me.call;

import java.util.Date;

import com.cenluan.activity.Activity;
import com.cenluan.activity.comment.ActivityComment;
import com.cenluan.blog.Blog;
import com.cenluan.blog.comment.BlogComment;
import com.cenluan.gallery.Gallery;
import com.cenluan.gallery.member.GalleryMember;
import com.cenluan.kit.UUIDKit;
import com.cenluan.tweet.Tweet;
import com.cenluan.tweet.comment.TweetComment;
import com.cenluan.user.User;
import com.cenluan.vote.Vote;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

public class Call extends Model<Call> {

    private static final long serialVersionUID = 1L;

    public static final Call dao = new Call();

    /**
     * 加入相册
     */
    public static final String TYPE_JOIN_GALLERY = "JOIN_GALLERY";
    /**
     * 参与投票
     */
    public static final String TYPE_JOIN_VOTE = "JOIN_VOTE";
    /**
     * 微博里提到
     */
    public static final String TYPE_TWEET_AT = "TWEET_AT";
    /**
     * 微博评论里提到
     */
    public static final String TYPE_TWEET_COMMENT_AT = "TWEET_COMMENT_AT";
    /**
     * 对微博进行评论
     */
    public static final String TYPE_POST_TWEET_COMMENT = "POST_TWEET_COMMENT";
    /**
     * 在博客评论里提到
     */
    public static final String TYPE_BLOG_COMMENT_AT = "BLOG_COMMENT_AT";
    /**
     * 发表博客评论
     */
    public static final String TYPE_POST_BLOG_COMMENT = "POST_BLOG_COMMENT";
    /**
     * 在活动评论里提到
     */
    public static final String TYPE_ACTIVITY_COMMENT_AT = "ACTIVITY_COMMENT_AT";
    /**
     * 发表活动评论
     */
    public static final String TYPE_POST_ACTIVITY_COMMENT = "POST_ACTIVITY_COMMENT";
    /**
     * 活动成员移除
     */
    public static final String TYPE_ACTIVITY_MEMBER_DELETED = "ACTIVITY_MEMBER_DELETED";
    /**
     * 报名活动被拒绝
     */
    public static final String TYPE_APPLY_ACTIVITY_REFUSED = "APPLY_ACTIVITY_REFUSED";
    /**
     * 报名活动被通过
     */
    public static final String TYPE_APPLY_ACTIVITY_PASSED = "APPLY_ACTIVITY_PASSED";

    /**
     * 保存
     *
     * @param from
     * @param to          要通知的用户id
     * @param type        此通知的类型
     * @param referenceId 关联内容的id，根据类型和此值可以找到关联内容
     * @return
     */
    public static boolean save(String from, String to, String type,
                               String referenceId) {
        User.dao.findById(to).clearNewsCache();
        Call call = new Call();
        call.set("id", UUIDKit.generateUUID());
        call.set("fromUserId", from);
        call.set("toUserId", to);
        call.set("type", type);
        call.set("referenceId", referenceId);
        call.set("read", false);
        call.set("createTime", new Date());
        return call.save();

    }

    /**
     * 获取被呼叫的用户
     *
     * @return
     */
    public User getToUser() {
        return User.dao.findById(getStr("toUserId"));
    }

    /**
     * 获取发起呼叫的用户
     *
     * @return
     */
    public User getFromUser() {
        return User.dao.findById(getStr("fromUserId"));
    }

    /**
     * 依据关联内容的id批量删除
     *
     * @param referenceId
     */
    public void deleteByReferenceId(String referenceId) {
        Db.update("delete from t_call where referenceId=?", referenceId);
    }

    /**
     * 获取关联的相册
     *
     * @return
     */
    public Gallery getGallery() {
        return Gallery.dao.findById(getStr("referenceId"));
    }

    /**
     * 获取关联的相册成员
     *
     * @return
     */
    public GalleryMember getGalleryMember() {
        return GalleryMember.dao.findById(getStr("referenceId"));
    }

    /**
     * 获取关联的投票
     *
     * @return
     */
    public Vote getVote() {
        return Vote.dao.findById(getStr("referenceId"));
    }

    /**
     * 获取关联的微博
     *
     * @return
     */
    public Tweet getTweet() {
        return Tweet.dao.findById(getStr("referenceId"));
    }

    /**
     * 获取关联的微博评论
     *
     * @return
     */
    public TweetComment getTweetComment() {
        return TweetComment.dao.findById(getStr("referenceId"));
    }

    /**
     * 获取关联博客
     *
     * @return
     */
    public Blog getBlog() {
        return Blog.dao.findById(getStr("referenceId"));
    }

    /**
     * 获取关联的博客评论
     *
     * @return
     */
    public BlogComment getBlogComment() {
        return BlogComment.dao.findById(getStr("referenceId"));
    }

    /**
     * 获取关联的活动
     *
     * @return
     */
    public Activity getActivity() {
        return Activity.dao.findById(getStr("referenceId"));
    }

    /**
     * 获取关联的活动评论
     *
     * @return
     */
    public ActivityComment getActivityComment() {
        return ActivityComment.dao.findById(getStr("referenceId"));
    }
}
